<template>
	<div class="diy_edit exam_edits" id="exam_edit">
	  <el-form
		:model="form"
		ref="ruleFormRef"
		:rules="rules"
		label-width="auto"
		class="edit_exam_form"
		size="large"
	  >
	  <el-row class="row_e">
		<el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
			<el-form-item label="名称" prop="name">
				<el-input v-model="form.name"></el-input>
			</el-form-item>
		</el-col>
		<el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
			<el-form-item label="答题时长" prop="duration">
				<el-input-number v-model="form.duration"></el-input-number>
			</el-form-item>
		</el-col>
		<el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
			<el-form-item label="总分" prop="score">
				<el-input-number v-model="form.score"></el-input-number>
			</el-form-item>
		</el-col>
		<el-col :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
			<el-form-item label="状态" prop="status">
				<el-select v-model="form.status" placeholder="请选择">
					<el-option label="启用" value="启用">
						启用
					</el-option>
					<el-option label="禁用" value="禁用">
						禁用
					</el-option>
				</el-select>
			</el-form-item>
		</el-col>
		</el-row>
		<div class="footer">
		  <el-button type="primary" @click="Onsubmit" size="large"
			>提交</el-button
		  >
		  <el-button @click="cancel()" size="large">取消</el-button>
		</div>
	  </el-form>
	</div>
  </template>
  
  <script setup>
  import { ElMessage } from "element-plus";
  import { ref, onMounted } from "vue";
  import { $post, $get } from "@/api/Api.js";
  import storageHelper from "@/libs/StorageHelper";
  import { useStore } from "vuex";
  import { useRouter } from "vue-router";
  import {
	$get_auth,
	func,
	paramsFun,
	$fullUrl,
	formatDate,
	$banseUrl,
	$check_action,
	$getUrlParams,
	filterSensitiveWords,
  } from "@/utils/utils.js";
  let router = useRouter();
  let store = useStore();
  let token = storageHelper.getToken() || null;
  let UserInfo = storageHelper.getUserInfo();
  let exam_id = $getUrlParams().exam_id;
  let user_group = storageHelper.getUserInfo().user_group;
  let user_id = storageHelper.getUserInfo().user_id;
  let url_add = ref("/api/exam/add");
  let url_set = ref("/api/exam/set");
  let url_get_obj = ref("/api/exam/get_obj");
  let title = ref("exam");
  let field = ref("exam_id");
  
  let form = ref({
	exam_id: 0,
	name: "",
	duration: 60,
	score: 0,
	status: "启用"
  });
  
  let ruleFormRef = ref(null);
  let rules = ref({
	name: [
	  {
		required: true,
		message: "名称不能为空",
		trigger: "blur",
	  },
	],
  });
  
  // 点击取消
  let cancel = () => {
	router.go(-1);
  };
  // 点击提交
  let Onsubmit = async () => {
	let valid = await ruleFormRef.value.validate();
	if (valid) {
	  if(exam_id){
		  // 编辑
		  delete form.value.update_time;
		  $post(url_set.value+"?exam_id=" + exam_id, form.value).then((res) => {
			  if (res.result) {
			  ruleFormRef.value.resetFields();
			  router.push("/home/exam/table");
			  ElMessage.success("提交成功！");
			  } else if (res.error) {
			  ElMessage.error(res.error.message);
			  }
		  });
	   
	  }else{
		  // 新增
		  $post(url_add.value, form.value).then((res) => {
			  if (res.result) {
			  ruleFormRef.value.resetFields();
			  router.push("/home/exam/table");
			  ElMessage.success("提交成功！");
			  } else if (res.error) {
			  ElMessage.error(res.error.message);
			  }
		  });
  
	  }
	} else {
	  ElMessage.error(msg);
	}
  };
  // 获取详情
  let get_exam_obj = async () => {
	var list = await $get("/api/exam/get_obj?exam_id=" + exam_id);
	form.value = list.result.obj;
  };
  onMounted(() => {
	if(exam_id){
	  get_exam_obj();
	}
  });
  </script>
  
  <style scoped lang="less">
  .diy_edit {
	width: 100%;
	.edit_exam_form {
	  max-width: 60%;
	  margin: 90px auto;
	  .fn {
		width: 100%;
	  }
	}
	img {
	  width: 100px;
	  height: 100px;
	}
	.edit_list_exam_sell {
	  width: 80%;
	  margin: 90px auto;
	}
	.footer {
	  text-align: center;
	}
  }
  </style>
  